/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.fileParserFramework.mimeType;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.jftstool.config.JFTSConfiguration;
import org.jftstool.config.exceptions.JFTSConfigException;
import org.jftstool.fileParserFramework.exceptions.MimeTypeException;
import org.semanticdesktop.aperture.mime.identifier.MimeTypeIdentifier;
import org.semanticdesktop.aperture.mime.identifier.magic.MagicMimeTypeIdentifier;
import org.semanticdesktop.aperture.util.IOUtil;


/**
 * Class responsible for getting the mime type of a file.
 * @author grupo JFTS
 *
 */
public class MimeTypeHandler {
	
	public MimeTypeHandler() { }
	
	/**
	 * Gets the mime type from a file.
	 * @param file
	 * @return String
	 * @throws MimeTypeException
	 */
	public String getMimeType(File file) throws MimeTypeException{
		String fileMimeType = null;
		MimeTypeIdentifier identifier = new MagicMimeTypeIdentifier();
		try {
			BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(file));
			byte[] bytes = IOUtil.readBytes(buffer, identifier.getMinArrayLength());
			fileMimeType = identifier.identify(bytes, file.getPath(), null);
			buffer.close();
		}
		catch (FileNotFoundException e) {
			throw new MimeTypeException("File no't found: " + file.getAbsolutePath());
		}
		catch (IOException e) {
			throw new MimeTypeException("Error to read file: " + file.getAbsolutePath());
		}		
		return fileMimeType;
	}
	
	
	/**
	 * Gets the mime type based on an InputStream.
	 * @param is
	 * @return String
	 * @throws MimeTypeException
	 */
	public String getMimeType(InputStream is) throws MimeTypeException{
		String fileMimeType = null;
		MimeTypeIdentifier identifier = new MagicMimeTypeIdentifier();
		try {
			BufferedInputStream buffer = new BufferedInputStream(is);
			byte[] bytes = IOUtil.readBytes(buffer, identifier.getMinArrayLength());
			fileMimeType = identifier.identify(bytes, "", null);
			buffer.close();
		}
		catch (IOException e) {
			throw new MimeTypeException("Error to determine mime type from inputStream");
		}		
		return fileMimeType;
	}
	

	/**
	 * Gets the mime type based on a string with the path of a file.
	 * @param file
	 * @return String
	 * @throws MimeTypeException
	 */
	public String getMimeType(String file) throws MimeTypeException{	
		return this.getMimeType(new File(file));
	}
	
	/**
	 * Devulve the extension associated to a mime type.
	 * @param mimeType
	 * @return String
	 * @throws MimeTypeException
	 */
	public String getExtensionAssociatedToMimeType(String mimeType) throws MimeTypeException{
		String extension = null;
		try {
			JFTSConfiguration mainConfig = JFTSConfiguration.getInstance();
			extension = mainConfig.getFPFConfig().getExtensionFromMimeType(mimeType);
		}
		catch (JFTSConfigException e) {
			throw new MimeTypeException(e.getMessage());			
		}
		return extension;
	}
}